#!/bin/bash

#! amount args
# ${_is_amount_arg} ${NARGS} min_equ max_equ
# if [ $? -ne 0 ]; then
#     plt_pause "from ${FNN}() :: ${_is_amount_arg} ${NARGS} min_equ:: be return 1"
#     return 1
# fi
#! normalize ptr_path -> absolut path
# ptr_path=$1
# ptr_path=$(${_abs_path} $PPWD "ptr_path") #ptr args
#! variables for cr_f_dir_insert_
# fn_name=$(${_prs_f} -n ${arg_path})
# root_path=$(${_prs_f} -d ${arg_path})
#! search in array
# [[ " ${arr[*]} " =~ " $1 " ]] && echo "true" || echo "false"
#! ${array[@]:1} all from 1 member arr

# echo -e "${GREEN}\${g_args[@]} = ${g_args[*]}${NORMAL}" #print variable
# echo -e "${GREEN}\${e_args[@]} = ${e_args[*]}${NORMAL}" #print variable
# echo -e "${GREEN}\${f_args[@]} = ${f_args[*]}${NORMAL}" #print variable
# echo -e "${GREEN}\${ARGS[@]} = ${ARGS[*]}${NORMAL}"     #print variable
echo -e "${GREEN}\${NARGS} = ${NARGS}${NORMAL}" #print variable
echo -e "${GREEN}\${ARGS0} = ${ARGS0}${NORMAL}" #print variable
echo -e "${GREEN}\${ARGS1} = ${ARGS1}${NORMAL}" #print variable
echo -e "${GREEN}\${ARGS2} = ${ARGS2}${NORMAL}" #print variable
echo -e "${GREEN}\${ARGS3} = ${ARGS3}${NORMAL}" #print variable
echo -e "${GREEN}\${ARGS4} = ${ARGS4}${NORMAL}" #print variable
echo -e "${GREEN}\${ARGS5} = ${ARGS5}${NORMAL}" #print variable
# echo -e "${GREEN}\${PPWD} = ${PPWD}${NORMAL}"           #print variable

# echo -e "${GREEN}\${dir_fns_} = ${dir_fns_}${NORMAL}" #print variable

# echo -e "${GREEN}\${var_wrap_arr[@]} = ${var_wrap_arr[*]}${NORMAL}" #print variable
# echo -e "${GREEN}\${var_fn_arr[@]} = ${var_fn_arr[*]}${NORMAL}"     #print variable

# echo -e "${GREEN}\${arg_fn_arr[@]} = ${arg_fn_arr[*]}${NORMAL}"
# echo -e "${GREEN}\${env_fn_arr[@]} = ${env_fn_arr[*]}${NORMAL}"
# echo -e "${GREEN}\${pts_fn_arr[@]} = ${pts_fn_arr[*]}${NORMAL}"

# echo -e "${GREEN}\$val_ppr = $val_ppr${NORMAL}" #print variable

# echo -e "${GREEN}\$ratis = $ratis${NORMAL}" #print variable

# echo "that ${FNN}.sal"

#! every from list_strong_cntls by able
# list_strong_cntls=(?str_ ?del_ ?parr_)
# for item in "${list_strong_cntls[@]}"; do
#     echo -e "${GREEN}\$item = $item${NORMAL}" #print variable
#     echo "$(eval echo \$"$item")"
#     if [ -z "$(eval echo \$"$item")" ]; then
#         plt_info "from ${FNN}() :: $item is empty :: be return 1"
#         return 1
#     fi
# done

#! once dpl_ or bcp_ mast be able (0 1)
# sum_1=$((${dpl_:-0} + ${bcp_:-0}))
# echo -e "${GREEN}\$sum_1 = $sum_1${NORMAL}"

# if [ "${sum_1:-0}" -ne 1 ]; then
#     plt_info "from ${FNN}() once dpl_ or bcp_ mast be able :: be return 1"
#     return 1
# fi

#! see ${PATH_CURR_REPO}/NBash/_man/ptr/flow_1.sh
#? in nid allready be reseiving value from ppr_
#   eval val_ppr=\$"${ppr_}" # reseiving value from ppr_
#? use and assign new value to ppr_
#   eval ${ppr_}=$(any_fn ${val_ppr}) # use old value of ppr_ and assign new value to ppr_

#! in pts_fn_arr contane external idetificators (pointers)
# num=
# echo num>pts.list
# any_fn(){num=10}
# any_fn --_pts pts.list
# any_fn
# echo ${num}
# 10

#! cr fs cr_f_
# cr_f_ \
#     "$filename" \
#     --tml "${PATH_TML_DIR}/cr_shf_/shf.tml" \
#     --fi0 "${header}" --fr0 "{header}" \
#     --vi0 "$filename" --vr0 "{filename}"

#! cr fs standart
# "${_obs_quis_}" "$filename"
# "${_upg_path}" "$filename"

#! cr fs cr_f_dir_insert_
# ${_cr_f_dir_insert_} --name ${filename} --dir_insert ${PATH_INSERT_DIR}/obs_quis_

#! innerfns
# if [ -d "${sil_}/proc/fol_" ]; then

#     c_up "${sil_}/proc/fol_"

#     inner_fn_arr=()
#     dr2parr_ inner_fn_arr 3 "${sil_}/proc/fol_"

# else
#     plt_exit "in ${FNN} : NOT_DIR : '${sil_}/proc/fol_' return 1"
#     return 1
# fi

# for item6 in ${inner_fn_arr[@]}; do
#     echo -e "${GREEN}\$item6 = $item6${NORMAL}" #print variable

#     ${item6}

#     ret_nom=$?
#     if [ ${ret_nom} -eq 2 ]; then
#         return 0
#     fi
#     if [ ${ret_nom} -eq 3 ]; then
#         return 1
#     fi
#     echo -e "${GREEN}\$ret_nom = $ret_nom${NORMAL}" #print variable

# done

#?--------------------------------------------------------
#?---------------------------------------------
#?---------------------------------------

echo -e "${GREEN}\${ARGS[@]:3} = ${ARGS[@]:3}${NORMAL}" #print variable

plt_info "dir with .lst sys_utils : file://${PLT_PATH}/.d/.lst/arb2mm_"

local dir_lst=${PLT_PATH}/.d/.lst/arb2mm_

#! START create ${dir_lst}/001_main_all.gn.lst (.gn create from generate)

local arb_path=
arb_path=${PD_PATH}/.d/.arb/proc.arb

local arr_main_tag_tmp=()
arr_main_tag_tmp=($(d2e_ -n -dd ${arb_path} gran))
# parr3e_ arr_main_tag_tmp

local main_tag=

true >${dir_lst}/001_main_all.gn.lst
for main_tag in ${arr_main_tag_tmp[@]}; do
    echo "${main_tag:4}" >>${dir_lst}/001_main_all.gn.lst
done

echo -e "${HLIGHT}--- cat file://${dir_lst}/001_main_all.gn.lst ---${NORMAL}" #start files
cat ${dir_lst}/001_main_all.gn.lst

#! END create ${PLT_PATH}/.d/.lst/arb2mm_/001_main_all.gn.lst

if isn_from__ ${NARGS} 2 10 "in ${FNN}() : ERR_AMOUNT_ARGS entered :'${NARGS}' args : return 1"; then
    return 1
fi

#! start b_sel_ar --------------------------------------
#! enter env menu <- ARGS0 , 'd2e_ 0 -ff ${PLT_PATH}/.d/.lst/arb2mm_ lst'
if ! is_num ${ARGS0}; then
    plt_exit "in ${FNN} : NOT_NUMBER : (num_menu)'${ARGS0}=${ARGS0}' : return 1"
    return 1
fi
if ! is_num ${ARGS1}; then
    plt_exit "in ${FNN} : NOT_NUMBER : (num_menu)'${ARGS1}=${ARGS1}' : return 1"
    return 1
fi
if ! is_num ${ARGS2}; then
    plt_exit "in ${FNN} : NOT_NUMBER : (num_menu)'${ARGS2}=${ARGS2}' : return 1"
    return 1
fi
local arr_gen=()

arr_gen=($(d2e_ 0 -ff ${dir_lst} lst))
arr_gen+=("edit_ file://${dir_lst}")

# parr3e_ arr_gen

if [[ -z "${arr_gen[0]}" ]]; then
    plt_info "generator return null : d2e_ 0 -ff file://${dir_lst} lst : return 0"
    return 0
fi

local num_res=
local already_define=0
local capta_lst=

if ! [ ${ARGS0} -eq 0 ]; then
    if [ ${ARGS0} -le ${#arr_gen[@]} ]; then
        num_res=$((${ARGS0} - 1))
        if [[ "edit_ file://${dir_lst}}" == "${arr_gen[${num_res}]}" ]]; then
            edit_ ${dir_lst}
            return 0
        fi
        capta_lst=${arr_gen[${num_res}]}
        already_define=1
    fi
fi
local item_gen
if [ ${ARGS0} -eq 0 ]; then
    if [ ${already_define} -eq 0 ]; then
        PS3="eligendi actiones: "
        select item_gen in "${arr_gen[@]}"; do
            if [[ "edit_ file://${dir_lst}" == "${item_gen}" ]]; then
                edit_ ${dir_lst}
                return 0
            fi
            capta_lst=$item_gen
            echo -e "${GREEN}\$capta_lst = $capta_lst${NORMAL}" #print variable
            break
        done
    fi
fi
#! result menu -> capta_lst
#! end b_sel_ar --------------------------------------
#[[b_sel_ar]]
echo -e "${GREEN}\$capta_lst = $capta_lst${NORMAL}" #print variable
#{main_body_fn}

#! start b_sel_ar --------------------------------------
#! enter env menu <- ARGS1 , f2e_ $capta_lst
if ! is_num ${ARGS1}; then
    plt_exit "in ${FNN} : NOT_NUMBER : (num_menu)'${ARGS1}=${ARGS1}' : return 1"
    return 1
fi
arr_gen=()
arr_gen=($(f2e_ $capta_lst))
if [[ -z "${arr_gen[0]}" ]]; then
    plt_info "generator return null : f2e_ $capta_lst : return 0"
    return 0
fi
num_res=
already_define=0
local capta_util_name=
if ! [ ${ARGS1} -eq 0 ]; then
    if [ ${ARGS1} -le ${#arr_gen[@]} ]; then
        num_res=$((${ARGS1} - 1))
        capta_util_name=${arr_gen[${num_res}]}
        already_define=1
    fi
fi
item_gen=
if [ ${ARGS1} -eq 0 ]; then
    if [ ${already_define} -eq 0 ]; then
        PS3="eligendi actiones: "
        select item_gen in "${arr_gen[@]}"; do
            # echo -e "${GREEN}$do_tm_file = $do_tm_file${NORMAL}" #print variable
            capta_util_name=$item_gen
            break
        done
    fi
fi
#! result menu -> capta_util_name
#! end b_sel_ar --------------------------------------

echo -e "${GREEN}\$capta_util_name = $capta_util_name${NORMAL}" #print variable

#! start grep for \$capta_util_name --------------------------------------

dir_proc_arb=${arb_path}

local ram_path=
local tag_file=
local arr_ram_util_path=()
local arr_main_str=()

for ram_path in $(d2e_ 0 -dd ${dir_proc_arb} ram); do

    tag_file=${ram_path}/tags.fol

    if cat ${tag_file} | grep @${capta_util_name}; then
        # echo -e "${GREEN}\$ram_path = $ram_path${NORMAL}" #print variable
        ram_name=$(prs_f -n ${ram_path})
        main_str=$(cat ${ram_path}/main.fol)
        tags_str="$("${_f2str_}" "${ram_path}"/tags.fol)"
        cont_str=$(cat "${ram_path}"/cont.fol)

        echo -e "
${GREEN}------------ START CONT ${RED}\${ram_name}: '${ram_name}' ${GREEN}------------${NORMAL}
${cont_str}  
${GREEN}------------ END CONT ${RED}\${ram_name}: '${ram_name}' ${GREEN}------------${NORMAL} 
${BLUE}PATH: file://${ram_path}${NORMAL}
${GREEN}NAME: ${ram_name}${NORMAL} 
${RED}MAIN: ${main_str}${NORMAL}
${CYAN}TAGS: ${tags_str}${NORMAL}"

        arr_ram_util_path+=("${ram_path}")
        arr_main_str+=("${main_str}")
    fi

done

#! end grep for \$capta_util_name --------------------------------------
#! result this : arr_main_str as main dimention , arr_ram_util_path as using to do
#[[b_sel_ar]]

echo -e "${GREEN}\${arr_ram_util_path[@]} = ${arr_ram_util_path[*]}${NORMAL}" #print variable
echo -e "${GREEN}\${arr_main_str[@]} = ${arr_main_str[*]}${NORMAL}"           #print variable

#! start bash select -----------------------------------
#! enter env menu: ${ARGS2} sa number menu, arr_main_str as name of points , arr_ram_util_path as using stinr
echo -e "${BLUE}--- exec ${FNN}() (num_menu) ---${NORMAL}" #started functions

num_res=
local item_arr=
local file_util_path=

echo -e "${GREEN}\${ARGS2} = ${ARGS2}${NORMAL}" #print variable

# arr_main_str+=("aaaaaaaaa")
# arr_ram_util_path+=("bbbbbbbbb")
if ! [ ${ARGS2} -eq 0 ]; then
    if [ ${ARGS2} -le ${#arr_main_str[@]} ]; then
        num_res=$((${ARGS2} - 1))
        file_util_path=${arr_ram_util_path[${num_res}]}
    fi
fi
if [ ${ARGS2} -eq 0 ]; then
    PS3="eligendi actiones: "
    IFS=$'\n'
    select item_arr in ${arr_main_str[@]}; do

        for ((i = 0; i < 1000; i++)); do
            if [[ ${item_arr} == ${arr_main_str[$i]} ]]; then
                file_util_path=${arr_ram_util_path[$i]}

            fi
        done
        break
    done
fi
#! end bash select -----------------------------------
#! result this : ${file_util_path}
echo -e "${GREEN}\$file_util_path = $file_util_path${NORMAL}" #print variable

echo -e "${GREEN}\${ARGS[@]:3} = ${ARGS[@]:3}${NORMAL}" #print variable

echo -e "${HLIGHT}--- . ${file_util_path}/cont.fol ${ARGS[@]:3} ---${NORMAL}" #start files
. ${file_util_path}/cont.fol "${ARGS[@]:3}"

#[[b_sel]]

#?---------------------------------------
#?---------------------------------------------
#?--------------------------------------------------------
